﻿// -------------------------------------------------------------------------
//    @FileName         :    NFMirrorSessionMgr.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-08-29
//    @Email			:    445267987@qq.com
//    @Module           :    NFMirrorSessionMgr
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFMirrorSessionMgr.h"

#include "NFLogicServer/NFLogicServerPlayerPlugin/Session/NFSession.h"
#include "NFLogicServer/NFLogicServerPlayerPlugin/Session/NFSessionUtil.h"

NFMirrorSessionMgr::NFMirrorSessionMgr()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFMirrorSessionMgr::~NFMirrorSessionMgr()
{
}

int NFMirrorSessionMgr::CreateInit()
{
    SetTimer(1000, 0, 0, 0, 0, 0);
    return 0;
}

int NFMirrorSessionMgr::ResumeInit()
{
    return 0;
}

int NFMirrorSessionMgr::GetLockedLoginMirrorSessionId(uint64_t roleId)
{
    auto iter = m_roleIdToSessionMap.find(roleId);
    if (iter != m_roleIdToSessionMap.end())
    {
        return iter->second;
    }

    return 0;
}

int NFMirrorSessionMgr::LockLoginMirror(uint64_t roleId, int sessionId)
{
    auto pSession = NFSessionUtil::GetSessionObj(sessionId);
    CHECK_NULL_WF(roleId, pSession, "GetSessionObj failed, sessionId: {}", sessionId);

    auto iter = m_roleIdToSessionMap.find(roleId);
    if (iter != m_roleIdToSessionMap.end())
    {
        LOG_ERR(roleId, -1, "roleId already locked login mirror, cannot lock again, roleId: {}, sessionId: {}", roleId, sessionId);
        return -1;
    }

    if (m_roleIdToSessionMap.full())
    {
        LOG_ERR(roleId, -1, "m_roleIdToSessionMap full, cannot lock login mirror, roleId: {}, sessionId: {}", roleId, sessionId);
        return -1;
    }

    m_roleIdToSessionMap[roleId] = sessionId;
    pSession->LockLoginMirror(roleId);
    return 0;
}

int NFMirrorSessionMgr::UnLockLoginMirror(uint64_t roleId, int sessionId)
{
    int oldSessionId = GetLockedLoginMirrorSessionId(roleId);
    if (oldSessionId == 0)
    {
        return 0;
    }

    if (oldSessionId != sessionId)
    {
        return -1;
    }

    m_roleIdToSessionMap.erase(roleId);

    LOG_INFO(roleId, "UnLockLoginMirror roleId:{} sessionId:{}", roleId, sessionId);

    auto pSession = NFSessionUtil::GetSessionObj(sessionId);
    if (pSession)
    {
        pSession->UnlockLoginMirror(UnlockMirrorSessionReason_Default);
    }

    return 0;
}

int NFMirrorSessionMgr::OnTimer(int timeId, int callCount)
{
    for (auto iter = m_roleIdToSessionMap.begin(); iter != m_roleIdToSessionMap.end();)
    {
        uint64_t roleId = iter->first;
        int sessionId = iter->second;

        auto pSession = NFSessionUtil::GetSessionObj(sessionId);
        if (!pSession)
        {
            LOG_INFO(roleId, "Login mirror session not exist, roleId: {}, sessionId: {}", roleId, sessionId);
            iter = m_roleIdToSessionMap.erase(iter);
            continue;
        }

        if (pSession->IsLockedLoginMirrorExpire())
        {
            LOG_INFO(roleId, "Login mirror lock expired, roleId: {}, sessionId: {}", roleId, sessionId);
            iter = m_roleIdToSessionMap.erase(iter);
            pSession->UnlockLoginMirror(UnlockMirrorSessionReason_Expire);
            continue;
        }

        ++iter;
    }

    return 0;
}
